<?php

function _poll_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  file_put_contents('log.log', $op."\n", FILE_APPEND);
  switch ($op) {
    case 'load':
      if ($node->type == 'poll') {
        if (!$node->field_poll_id[0]['value']) {
          $node->field_poll_id[0]['value'] = _auto_generate_poll_id($node);
        }
      }
      break;
      case 'validate':
        $query = db_query("SELECT node.nid FROM node node WHERE node.title='%s' AND node.nid !=%d AND node.status=1", $node->title, $node->nid);
        if ($nid = db_result($query)) {
          form_set_error('title', "Duplicate Vote Topic");
        }
        break;
  }
}

function _auto_generate_poll_id($node) {
  $base_poll_id = $node->type . '-' . $node->created . '-' . $node->id;
  return md5($base_poll_id);
}